package opengl.program;
import static org.lwjgl.opengl.GL11.GL_FALSE;
import static org.lwjgl.opengl.GL20.GL_COMPILE_STATUS;
import static org.lwjgl.opengl.GL20.GL_INFO_LOG_LENGTH;
import static org.lwjgl.opengl.GL20.glCompileShader;
import static org.lwjgl.opengl.GL20.glCreateShader;
import static org.lwjgl.opengl.GL20.glGetShaderInfoLog;
import static org.lwjgl.opengl.GL20.glGetShaderi;
import static org.lwjgl.opengl.GL20.glShaderSource;
import static org.lwjgl.opengl.GL20.GL_VERTEX_SHADER;
import static org.lwjgl.opengl.GL20.GL_FRAGMENT_SHADER;
import file.FileUtil;

public class GLShader {
	public static int createVertexShader(Object o, String fileName) throws Exception {
		return create(o, GL_VERTEX_SHADER, fileName);
	}

	public static int createFragmentShader(Object o, String fileName) throws Exception {
		return create(o, GL_FRAGMENT_SHADER, fileName);
	}

	private static int create(Object o, int shaderType, String fileName) throws Exception {
		int vs = glCreateShader(shaderType);
		glShaderSource(vs, FileUtil.read(o.getClass(), fileName));
		glCompileShader(vs);
		if (glGetShaderi(vs, GL_COMPILE_STATUS) == GL_FALSE) {
			throw new Exception("Failure in compiling shader(" + fileName + "). Error log:\n"
					+ glGetShaderInfoLog(vs, glGetShaderi(vs, GL_INFO_LOG_LENGTH)));
		}
		return vs;
	}

}
